﻿/*
 * 创建合同UI。V2。
 * 
 * 合同一经签订，不可修改。
 * 
 * 
 * */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Model;
using EQT.Dal;
using DevFuture.Common;

public partial class CreateContractV2 : System.Web.UI.Page
{
    private DaProductCheckContract dapcc = WorkContext.AppCtx.GetObject("DaProductCheckContract") as DaProductCheckContract;
    private DaTuser dauser = WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Utility.SetButtonStyle(SiteConfig.Common_Button_Css, Button_Save, Button_Save_Ex, Button_Cancel);
            txtCheckDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
    
    /// <summary>
    /// 保存合同，并显示出View状态的合同信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button_Save_Click(object sender, EventArgs e)
    {
        MoProductCheckContract moPcc = new MoProductCheckContract();
        if (Form2Model(ref moPcc))
        {
            moPcc.ReportStatus = (int)ContractStatus.Signed;//合同状态为：1=草稿，2=已签订/未送检  
            //保存合同基本信息到Db ，合同未送检         
            dapcc.Add(moPcc);
            //重定向到合同情况查看页面
            Response.Redirect(String.Format("PCCInfo.aspx?report_id={0}", moPcc.ReportId));
        }
    }

    /// <summary>
    /// 分配合同号，将UI的值赋到实体上
    /// </summary>
    /// <param name="moPcc"></param>
    /// <returns></returns>
    private bool Form2Model(ref MoProductCheckContract moPcc)
    {
        //JavaScript没有更改视图状态
        string pca_id = Request.Form[textBox_PCA_ID.UniqueID];// textBoxClientID.Text; 
        textBox_PCA_ID.Text = pca_id;
        if (string.IsNullOrEmpty(pca_id))
        {
            return false;
        }

        UserContext uc = Session["USER_CONTEXT"] as UserContext;
        string client_id = Request.Form[textBoxClientID.UniqueID];// textBoxClientID.Text; 
        textBoxClientID.Text = client_id;
        //以下10个字段从页面获得
        if (!string.IsNullOrEmpty(client_id))
        {
            moPcc.ClientId = textBoxClientID.Text;
        }       
        moPcc.Addr = textBox_Addr.Text;      
        moPcc.Consigner = textBoxConsigner.Text;
        moPcc.CorpName = textBoxCorp_Name.Text;       
        moPcc.Cuid = uc.UserID;
        moPcc.Email = textBoxEmail.Text;
        moPcc.Fax = textBoxFax.Text;        
        moPcc.Phone=textBoxPhone.Text;      
        moPcc.Postcode=textBox_Postcode.Text;
        moPcc.Linkman = textBoxLinkman.Text;
        //产品信息来源于能力表，无需重复记录，只需记录PCA_ID
        moPcc.PcaId = textBox_PCA_ID.Text;
        //创建了新的合同ID
        if (!string.IsNullOrEmpty(txtSelfCode.Text.Trim()))
        { 
            //检查手工输入编号是否重复。
            if (!dapcc.CheckUserInputID(txtSelfCode.Text.Trim()))
            {
                moPcc.ReportId = txtSelfCode.Text.Trim();
            }
            else
            {
              
                ClientScript.RegisterStartupScript(typeof(Page),"startup", "<script language = 'javascript'>alert('合同编号重复！请重新输入。');</script>");
                return false;
            }
        }
        else
        {
            moPcc.ReportId = SequenceKeys.GenerateContractID();
        }
        decimal charge = 0;
        decimal.TryParse(textBox_Charge.Text, out charge);
        moPcc.Charge = charge;
        moPcc.CreateTime = DateTime.Now;
        moPcc.PcaRem = textBox_PCA_Rem.Text;


        moPcc.Class1 = Request["ctl00$ContentPlaceHolder$txtCheckDate"].ToString();


        //其他字段在创建阶段无需赋值
        return true;
    }

    /// <summary>
    /// 保存并送检 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button_Save_Ex_Click(object sender, EventArgs e)
    {
        MoProductCheckContract moPcc = new MoProductCheckContract();
        if (Form2Model(ref moPcc))
        {
            moPcc.ReportStatus = (int)ContractStatus.WaitTest;//合同状态为：1=草稿，2=已签订/未送检 ,16为已经送检
            try
            {
                dapcc.AddPccAndCreateTask(moPcc, WorkContext.AppCtx.GetObject("DaProductCheckAbility") as DaProductCheckAbility,
                    WorkContext.AppCtx.GetObject("DaContractTask") as DaContractTask);

                Response.Redirect(String.Format("PCCInfo.aspx?report_id={0}", moPcc.ReportId));

            }
            catch (DalException ex)
            {
                Utility.SiteError(String.Format("保存合同并送检失败:{0}", ex.Message));
            }
            
        }
    }

    /// <summary>
    /// 取消则返回到合同查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("PCCQuery.aspx");
    }


    protected void SetUIByRights()
    {
        //在这里添加内部权限检查，并放到对象初始化代码之后
    }

}
